/**
 * author bianlongting
 * email  1137060420@qq.com
 * date   2016-04-14
 * time   14:36
 * description 广告管理
 */

$(function(){
    var data = {};
    var pageIndex = 1;	//页码默认为第一页
    var brandid = $('.brandid').val();
    var type = $('input[name="type"]').val();
    data.type = type;
    data.brandid = brandid;
    getGoods(data);

    function addsetGood(){
        $('.select-content tr').each(function(){
            var id = $(this).data().id;
            var html =  '<input type="checkbox" data-id="' + id + '">';
            $(this).find('.input-check').html(html);
        });
    }
    // 翻页
    function setPage(num){
        $('#page').find('a').on('click',function(e){
            e.preventDefault();
            var that = $(this);
            var parent =that.parent();
            if (parent.hasClass("next")) {
                if (pageIndex == num) {
                    return false;
                } else {
                    pageIndex++;
                }
            } else if (parent.hasClass("prev")){
                if (pageIndex == 1) {
                    return false;
                } else {
                    if (that.text() == 'First') {
                        pageIndex = 1;
                    } else{
                        pageIndex --;
                    }
                }
            } else {
                pageIndex = parseInt(that.text());
            }

            data.p = pageIndex;
            getGoods(data) ;
        });
    }


    // 请求商品数据
    function getGoods(data){
        $.ajax({
            url : '/index.php?m=Home&c=Activity%2FCreateAdvert&a=ajaxGetBrandGoods',
            data : data,
            type : 'GET',
            dataType : 'JSON',
            success : function(data){
                if (data.success == true) {
                    if (!$.isArray(data.data)){
                        var pagehtml = data.data.pageHtml;
                        var num = Math.ceil(parseInt(data.data.totalNum)/5);
                        var data = data.data.goodList;

                        goodRender(data,pagehtml,num);
                        addsetGood();
                    } else {
                        $('.good-content').children().remove();
                        $('#page').children().remove();
                        addsetGood();
                    }

                } else {
                    alert(data.message);
                }
            }

        });

    }


    // 拼装页面
    function goodRender(param,pageHtml,num){
        var html = '';
        var goodArray = [];
        $('.select-content').find('input').each(function() {
            goodArray.push($(this).data().id);
        });
        for (var i = 0;i < param.length;i++) {
            for (var j = 0; j < goodArray.length; j++) {
                if (param[i].id == goodArray[j]) {
                    delete param[i].id;
                };
            };
            if ( param[i].id !=undefined) {
                html +='<tr align="center" data-id = ' + param[i].id + '>\
                        <td class="input-check"><input type="checkbox" data-id = ' + param[i].id + '></td>\
                        <td>' + param[i].id + '</td>\
                        <td>' + param[i].name + '</td>\
                        <td>' + param[i].sn + '</td>\
                        <td>' + param[i].price + '</td>\
                        <td>' + param[i].stock + '</td>\
                    </tr>';
            }

        }

        $('.good-content').html(html);
        $('#page').html(pageHtml);
        setPage(num);

    }

    // 翻页
    function setPage(num){
        $('#page').find('a').on('click',function(e){
            e.preventDefault();
            var that = $(this);
            var parent =that.parent();
            if (parent.hasClass("next")) {
                if (pageIndex == num) {
                    return false;
                } else {
                    pageIndex++;
                }
            } else if (parent.hasClass("prev")){
                if (pageIndex == 1) {
                    return false;
                } else {
                    if (that.text() == 'First') {
                        pageIndex = 1;
                    } else{
                        pageIndex --;
                    }
                }
            } else {
                pageIndex = parseInt(that.text());
            }

            data.p = pageIndex;
            getGoods(data) ;
        });
    }

    // 添加商品
    $('.btn-setgood').on('click',function(){
        var goodArr = [];
        var tr = $('.good-content').find('input');
        tr.each(function(){
            if ($(this).prop("checked")){
                goodArr.push($(this).parents('tr'));
            }
        });
        if (goodArr.length > 0) {
            for(var i =0 ;i < goodArr.length;i++){
                $('.select-content').append(goodArr[i]);
            }
            $('.select-content input').prop("checked",false);
        } else {
            alert('请选择商品');
        }
    });

    // 删除商品
    $('.btn-deletegood').on('click',function(){
        var tr =  $('.select-content tr');
        if (tr.length > 0) {
            tr.each(function(){
                $(this).find("input").each(function(){
                    if ($(this).prop("checked")) {
                        $(this).parents("tr").remove();
                    };
                })
            });

        } else {
            alert('请选择要删除的商品');
            return false;
        }
    });

    $('.btn-add').on('click',function(){
        var good = $('.select-content').find('input');
        var selectGoodArr = [];
        good.each(function(){
            selectGoodArr.push($(this).data().id);
        });
        if (selectGoodArr.length > 0) {
            $("input[name='goods']").val(selectGoodArr.toString());

        } else {
            $("input[name='goods']").val('');
        }
        $('form').submit();
    });


    //toast提示函数
    function toast(tip){
        $(".toast_tip").html(tip);
        $(".toast").fadeIn('1000');
        setTimeout(function(){
            $(".toast").fadeOut();
        },1000);
        return false;
    }
})